package main

import "fmt"

func main() {
	//arr := []int{5,4,3,2,1}
	arr1 := []int{3,2,45,612,1,23,45,6}

	fmt.Println("arr1 未排序 1：",arr1)
	BubbleSort(arr1)
	fmt.Println("arr1 冒泡排序后 1：",arr1)

	fmt.Println("arr1 未排序 2：",arr1)
	BubbleSort2(arr1)
	fmt.Println("arr1 冒泡排序后 2：",arr1)
}

func BubbleSort(arr []int)  {
	iCount :=0 //outer
	jCount :=0 //inner
	for i:=0;i<len(arr)-1;i++{
		flag := true   //if no switch operation ,we can break
		for j:=0;j<len(arr)-1-i;j++{
			if arr[j]>arr[j+1]{
				arr[j],arr[j+1] = arr[j+1],arr[j]
				flag = false
			}
			jCount++
		}
		iCount++
		if flag{
			break
		}
	}
	fmt.Println("i循环的次数 iCount:",iCount)
	fmt.Println("j循环的次数 jCount:",jCount)
}


func BubbleSort2(arr []int)  {
	iCount :=0 //outer
	jCount :=0 //inner
	for i:=0;i<len(arr)-1;i++{
		for j:=0;j<len(arr)-1-i;j++{
			if arr[j]>arr[j+1]{
				arr[j],arr[j+1] = arr[j+1],arr[j]
			}
			jCount++
		}
		iCount++
	}
	fmt.Println("i循环的次数 iCount:",iCount)
	fmt.Println("j循环的次数 jCount:",jCount)
}